package cn.gailvlun.gll.widget.dialog;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;

import com.bigkoo.pickerview.adapter.NumericWheelAdapter;
import com.bigkoo.pickerview.lib.WheelView;
import com.bigkoo.pickerview.listener.OnItemSelectedListener;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import cn.gailvlun.gll.net.R;
import cn.gailvlun.gll.util.LogUtil;



public class DatePicker extends LinearLayout {
    public DatePicker(Context context) {
        this(context, null);
    }

    public DatePicker(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DatePicker(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        inflate(getContext(), R.layout.picker_date, this);
        year = findViewById(R.id.year);
        month = findViewById(R.id.month);
        day = findViewById(R.id.day);

        this.year.setCyclic(false);
        this.year.setTextSize(14);

        this.month.setCyclic(false);
        this.month.setTextSize(14);

        this.day.setCyclic(false);
        this.day.setTextSize(14);
    }

    private WheelView year;
    private WheelView month;
    private WheelView day;

    private NumericWheelAdapter yearAdapter;
    private NumericWheelAdapter monthAdapter;
    private NumericWheelAdapter dayAdapter;

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (isInEditMode()) {
            setFix(DateTime.now());
        }
    }

    private DateTime mStartDate;
    private DateTime mEndDate;
    private DateTime mSelectDate;

    public void setFix(DateTime dateTime) {
        yearAdapter = new NumericWheelAdapter(dateTime.getYear(), dateTime.getYear());
        monthAdapter = new NumericWheelAdapter(dateTime.getMonthOfYear(), dateTime.getMonthOfYear());
        dayAdapter = new NumericWheelAdapter(dateTime.getDayOfMonth(), dateTime.getDayOfMonth());

        this.year.setAdapter(yearAdapter);
        this.month.setAdapter(monthAdapter);
        this.day.setAdapter(dayAdapter);
    }

    public void setRange(final DateTime startTime, final DateTime endTime) {
        mStartDate = startTime.withTime(0, 0, 0, 0);
        mEndDate = endTime.withTime(0, 0, 0, 0);

        yearAdapter = new NumericWheelAdapter(startTime.getYear(), endTime.getYear());
        if (startTime.getYear() == endTime.getYear()) {
            monthAdapter = new NumericWheelAdapter(startTime.getMonthOfYear(), endTime.getMonthOfYear());
        } else {
            monthAdapter = new NumericWheelAdapter(startTime.getMonthOfYear(), startTime.monthOfYear().getMaximumValue());
        }
        if (startTime.getMonthOfYear() == endTime.getMonthOfYear() && startTime.getYear() == endTime.getYear()) {
            dayAdapter = new NumericWheelAdapter(startTime.getDayOfMonth(), endTime.getDayOfMonth());
        } else {
            dayAdapter = new NumericWheelAdapter(startTime.getDayOfMonth(), startTime.dayOfMonth().getMaximumValue());
        }

        this.year.setAdapter(yearAdapter);
        this.year.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                Integer selectedYear = (Integer) yearAdapter.getItem(index);
                resetMonthAdapter(selectedYear);
            }
        });
        this.month.setAdapter(monthAdapter);
        this.month.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                Integer selectedYear = (Integer) yearAdapter.getItem(year.getCurrentItem());
                Integer selectedMonth = (Integer) monthAdapter.getItem(index);
                resetDayAdapter(selectedYear, selectedMonth);
            }
        });
        this.day.setAdapter(dayAdapter);
        this.day.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                if (mOnDayChangeListener != null) {
                    mOnDayChangeListener.onDayChange(getDate());
                }
            }
        });
    }

    public interface OnDayChangeListener {
        void onDayChange(DateTime dateTime);
    }

    public OnDayChangeListener mOnDayChangeListener;

    public void setOnDayChangeListener(OnDayChangeListener onDayChangeListener) {
        mOnDayChangeListener = onDayChangeListener;
    }

    private void resetMonthAdapter(int selectedYear) {

        if (mStartDate.getYear() == mEndDate.getYear()) {
            return;
        } else {
            if (mStartDate.getYear() == selectedYear) {
                monthAdapter = new NumericWheelAdapter(mStartDate.getMonthOfYear(),
                        mStartDate.monthOfYear().getMaximumValue());
            } else if (mEndDate.getYear() == selectedYear) {
                monthAdapter = new NumericWheelAdapter(1, mEndDate.getMonthOfYear());
            } else {
                monthAdapter = new NumericWheelAdapter(1, DateTime.now().withDate(
                        (Integer) yearAdapter.getItem(year.getCurrentItem()),
                        1,
                        1).monthOfYear().getMaximumValue());
            }
            month.setAdapter(monthAdapter);
        }
    }

    private void resetDayAdapter(int selectedYear, int selectedMonth) {


        if (mStartDate.getYear() == mEndDate.getYear() &&
                mStartDate.getMonthOfYear() == mEndDate.getMonthOfYear()) {
            return;
        } else {
            if (mStartDate.getYear() == selectedYear &&
                    mStartDate.getMonthOfYear() == selectedMonth) {
                dayAdapter = new NumericWheelAdapter(mStartDate.getDayOfMonth(),
                        mStartDate.dayOfMonth().getMaximumValue());
            } else if (
                    mEndDate.getYear() == selectedYear &&
                            mEndDate.getMonthOfYear() == selectedMonth) {
                dayAdapter = new NumericWheelAdapter(1, mEndDate.getDayOfMonth());
            } else {
                dayAdapter = new NumericWheelAdapter(1, DateTime.now().withDate(
                        (Integer) yearAdapter.getItem(year.getCurrentItem()),
                        (Integer) monthAdapter.getItem(month.getCurrentItem()),
                        1).dayOfMonth().getMaximumValue());
            }
            day.setAdapter(dayAdapter);
            if (day.getCurrentItem() >= dayAdapter.getItemsCount()) {
                day.setCurrentItem(day.getItemsCount() - 1);
            }
        }
    }

    public void setSelectDate(DateTime selectDate) {
        
        mSelectDate = selectDate.withTime(0, 0, 0, 0);

        if (mStartDate == null || mEndDate == null) {
            return;
        }
        if (mSelectDate.compareTo(mStartDate) >= 0 && mSelectDate.compareTo(mEndDate) <= 0) {
            
            year.setCurrentItem(yearAdapter.indexOf(mSelectDate.getYear()));
            resetMonthAdapter(mSelectDate.getYear());
            month.setCurrentItem(monthAdapter.indexOf(mSelectDate.getMonthOfYear()));
            resetDayAdapter(mSelectDate.getYear(), mSelectDate.getMonthOfYear());
            day.setCurrentItem(dayAdapter.indexOf(mSelectDate.getDayOfMonth()));
        }
    }

    public DateTime getDate() {
        return DateTime.now()
                .withYear((Integer) yearAdapter.getItem(year.getCurrentItem()))
                .withMonthOfYear((Integer) monthAdapter.getItem(month.getCurrentItem()))
                .withDayOfMonth((Integer) dayAdapter.getItem(day.getCurrentItem()));
    }

}
